<?php
/*
 * Created on 20.12.2005
 *
 */
 
include_once($GLOBALS['cfgPaths']['lib'].'model2/FilterChain.o.inc.php');
 
class Model2 {
    
    var $basedirCache;

    function Model2 (&$env, $config, $basedir, $defaultaction, $defaulterroraction, $classfileending = '.o.inc.php', $viewfileending = '.inc.php') {
        $this->attrs = $config;
        $this->attrs['classfileending'] = $classfileending;
        $this->attrs['viewfileending'] = $viewfileending;
        $this->attrs['basedir'] = $basedir;
        $matches = array();
        // caching of basedir for nested apps
        if (isset($GLOBALS['MODEL2_BASEDIR']))
            $this->basedirCache = $GLOBALS['MODEL2_BASEDIR'];
        $GLOBALS['MODEL2_BASEDIR'] = $basedir;
        $this->attrs['defaultaction']['file'] = $defaultaction;
        preg_match('/(\w+)$/',$defaultaction,$matches);
        $this->attrs['defaultaction']['class'] = $matches[1];
        $this->attrs['defaulterroraction']['file'] = $defaulterroraction;
        preg_match('/(\w+)$/',$defaulterroraction,$matches);
        $this->attrs['defaulterroraction']['class'] = $matches[1];
        $this->filterchain = new FilterChain($env, $basedir, $this->attrs);    
    }
    
    /**
     * @param array filters Array of strings
     */
    function addFilters ($filters) 
    {
        foreach ($filters as $filter) 
        {
            $this->registerFilter($filter);
        }
    }
    
    /**
     * @param array actions Assoz. array of strings 
     */
    function addActions ($actions) 
    {
        foreach ($actions as $n => $v)
        {
            $this->registerAction($n, $v);
        }
    }
    
    function registerAction($name, $actionclass)
    {
        $matches = array();
        $this->attrs['actiondict'][$name]['file'] = $actionclass;
        preg_match('/(\w+)$/',$actionclass,$matches);
        $this->attrs['actiondict'][$name]['class'] = $matches[1];
    }
    
    function registerFilter($filter) 
    {
        $this->filterchain->addFilter($filter);
    }
    
    function start () {
        $this->filterchain->startChain();
        // writing back basedir cached for nested apps
        if (isset($this->basedirCache))
            $GLOBALS['MODEL2_BASEDIR'] = $this->basedirCache;
    }

}

?>
